package com.zcxy360.sandtable.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcxy360.sandtable.domain.Drawing;
import com.zcxy360.sandtable.domain.DrawingExample;
import com.zcxy360.sandtable.domain.DrawingExample.Criteria;
import com.zcxy360.sandtable.domain.DrawingMapper;
import com.zcxy360.sandtable.domain.User;
import com.zcxy360.sandtable.domain.VdrawingMapper;
import com.zcxy360.sandtable.util.ShiroUtil;
import com.zcxy360.sandtable.util.StringUtil;

@Transactional
@Service
public class DrawingService {

	@Autowired
	private DrawingMapper drawingMapper;
	@Autowired
	private VdrawingMapper vdrawingMapper;

	public Object getByDrawingId(Integer drawingId,Integer deep) {
		Drawing drawing = null;
		switch (deep) {
		case 0:
			drawing = drawingMapper.selectByPrimaryKey(drawingId);
			break;
		case 1:
			drawing = drawingMapper.selectByPrimaryKeyWithExtra(drawingId);
			break;
		default:
			break;
		}
		return drawing;
	}

	public Object list(Drawing drawing, String key, String orderByClause,Integer sign,Integer pageNum, Integer pageSize, Integer navigatePages) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			drawing.setOrgId(loginUser.getOrgId());
		}
		if(sign != null) {
			//只看自己
			if(sign == 1 && !loginRole.equals("admin")) {
				drawing.setUserId(loginUser.getUserId());
			}
			//看非自己的其他老师资源
			if(sign == 2) {
				drawing.setUserId(loginUser.getUserId());
			}
			//看内建资源
			if(sign == 3) {
				drawing.setUserId(1);
				drawing.setOrgId(null);
			}
			//看本机构自己的所有资源及其它老师共享资源及内建资源
			if(sign == 4) {
				drawing.setUserId(loginUser.getUserId());
			}
			//看本机构所有资源及内建资源
			if(sign == 5) {
			}
		}
		PageHelper.startPage(pageNum,pageSize);
		List<Drawing> drawings = drawingMapper.listWithExtra(drawing,StringUtil.wrapper(key),orderByClause,sign);
		return new PageInfo<>(drawings, navigatePages);
	}

	public Drawing post(Drawing drawing) {
		User loginUser = ShiroUtil.getUser();
		drawing.setUserId(loginUser.getUserId());
		drawing.setOrgId(loginUser.getOrgId());
		drawing.setRoughTime(10);
		drawingMapper.insertSelective(drawing);
		vdrawingMapper.insertByDrawingId(drawing.getDrawingId());
		return drawing;
	}

	public boolean update(Drawing drawing) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			Drawing d = drawingMapper.selectByPrimaryKey(drawing.getDrawingId());
			if(loginRole.equals("teacher")) {
				if(!d.getUserId().equals(loginUser.getUserId())) {
					throw new RuntimeException("老师只能修改本人的资源");
				}
			}
			if(loginRole.equals("manager")) {
				if(!d.getOrgId().equals(loginUser.getOrgId())) {
					throw new RuntimeException("机构管理员只能修改本机构的资源");
				}
			}
		}
		drawingMapper.updateByPrimaryKeySelective(drawing);
		vdrawingMapper.insertByDrawingId(drawing.getDrawingId());
		return true;
	}

	public boolean delete(int[] drawingIds) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		Drawing d;
		if(!loginRole.equals("admin")) {
			for(int drawingId:drawingIds) {
				d = drawingMapper.selectByPrimaryKey(drawingId);
				if(loginRole.equals("teacher")) {
					if(!d.getUserId().equals(loginUser.getUserId())) {
						throw new RuntimeException("老师只能删除本人的资源");
					}
				}
				if(loginRole.equals("manager")) {
					if(!d.getOrgId().equals(loginUser.getOrgId())) {
						throw new RuntimeException("机构管理员只能删除本机构的资源");
					}
				}
			}
		}
		drawingMapper.deleteBatch(drawingIds);
		return true;
	}

	public boolean checkName(String name) {
		User loginUser = ShiroUtil.getUser();
		DrawingExample ex = new DrawingExample();
		Criteria or = ex.or();
		or.andNameEqualTo(name);
		if(loginUser.getRole().equals("admin")) {
			or.andOrgIdIsNull();
		}else {
			or.andOrgIdEqualTo(loginUser.getOrgId());
		}
		return drawingMapper.countByExample(ex) == 0;
	}

}
